using Microsoft.AspNetCore.Mvc;
using System.Collections.Generic;
using ApiTest.Api.Entity;
using System.Linq;
using ApiTest.Api.Db;
using ApiTest.Api.Repository;
using ApiTest.Api.Params;
using Microsoft.Extensions.Configuration;
using ApiTest.Api.Utils;
using Microsoft.AspNetCore.Authorization;
using Admin3000.Backend.Api.Params;

namespace ApiTest.Api.Controllers
{
    // [Authorize]
    [ApiController]
    [Route("[controller]")]
    public class SpuspecificationController : ControllerBase
    {
        private IConfiguration _configuration;
        private IRepository<Spuspecification> _SpuspecificationRepository;
        private TokenParameter _tokenParameter;

        public SpuspecificationController(IConfiguration configuration, IRepository<Spuspecification> SpuspecificationRepository)
        {
            _configuration = configuration;
            _SpuspecificationRepository = SpuspecificationRepository;
            _tokenParameter = _configuration.GetSection("TokenParameter").Get<TokenParameter>();
        }

        //列出商品列表
        [HttpGet]
        public dynamic Get()
        {
            var Spuspecification = _SpuspecificationRepository.Table.ToList();
            var res = new
            {
                Code = 1000,
                Data = new { Data = Spuspecification },
                Msg = "获取列表成功^_^"
            };

            return JsonHelper.Serialize(res);
        }


        [HttpGet("{id}")]
        public dynamic Get(int id)
        {
            var spuspecification = _SpuspecificationRepository.GetById(id);

            return JsonHelper.Serialize(new
            {
                Code = 1000,
                Data = spuspecification,
                Msg = "查询指定用户成功"
            });
        }

        [HttpPost]
        public dynamic Post(CreateSpuspecification newSpuspecification)
        {
            var Spuspecification = new Spuspecification
            {
                Spuspecificationname = newSpuspecification.Spuspecificationname,
                SpuId = newSpuspecification.SpuId,
            };
            _SpuspecificationRepository.Insert(Spuspecification);
            return JsonHelper.Serialize(new
            {
                Code = 1000,
                Data = Spuspecification,
                Msg = "添加商品规格成功"
            });
        }

        [HttpPut("{id}")]
        public dynamic Put(int id, CreateSpuspecification updateSpuspecification)
        {
            var Spuspecificationname = updateSpuspecification.Spuspecificationname.Trim();
            var spuspecification = _SpuspecificationRepository.GetById(id);
            if (spuspecification == null)
            {
                return JsonHelper.Serialize(new
                {
                    Code = 104,
                    Data = "",
                    Msg = "查询不到该用户，无法更新，请重试"
                });
            }

            spuspecification.Spuspecificationname = updateSpuspecification.Spuspecificationname;
            spuspecification.SpuId = updateSpuspecification.SpuId;
            // user.IsActived= updateUser.IsActived;

            _SpuspecificationRepository.Update(spuspecification);

            return JsonHelper.Serialize(new
            {
                Code = 1000,
                Data = spuspecification,
                Msg = "更新成功"
            });


        }

        [HttpDelete("{id}")]
        public dynamic Delete(int id)
        {
            _SpuspecificationRepository.Delete(id);

            return new
            {
                Code = 1000,
                Data = "",
                Msg = "删除成功"
            };


        }
    }

}
